/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presenter;

import boundary.LoginView;
import controladora.AutenticaCliente;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

/**
 *
 * @author jakson
 */
public class LoginPresenter {

    private LoginView view;
    private controladora.AutenticaCliente autenticar = new AutenticaCliente();
    private presenter.AutenticaDiretorRegionalPresenter auten = new AutenticaDiretorRegionalPresenter();
    private presenter.AutenticarGerente aut = new AutenticarGerente();

    public LoginPresenter() {

        view = new LoginView();
        view.getBtnSair().addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                fechar();
            }
        });

        view.getBtnEntrar().addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                entrar();
            }
        });

        view.setLocationRelativeTo(view);
        view.setVisible(true);
    }

    public void fechar() {
        view.dispose();
    }

    private void limparCampos() {
        view.getCampoSenha().setText("");
        view.getUsuario().setText("");


    }

    public void entrar() {



        if (view.getCampoCargo().getSelectedItem().toString().equals("Cliente")) {
            if (autenticar.autentica(view.getUsuario().getText(), view.getCampoSenha().getText()) == true) {
                String usuario = view.getUsuario().getText();
                limparCampos();
                new ClientePresenter(usuario);

            }
        }

        if (view.getCampoCargo().getSelectedItem().toString().equals("Diretor Regional")) {
            if (auten.autentica(view.getUsuario().getText(), view.getCampoSenha().getText()) == true) {
                limparCampos();
                new DirRegionalPresenter();

            }
        }

        if (view.getCampoCargo().getSelectedItem().toString().equals("Gerente")) {
            if (aut.autentica(view.getUsuario().getText(), view.getCampoSenha().getText()) == true) {
                limparCampos();
                new GerentePresenter();

            }
        }

        if (view.getCampoCargo().getSelectedItem().toString().equals("Diretor Geral")) {
            if (view.getUsuario().getText().equals("") || view.getCampoSenha().getText().equals("")) {
                JOptionPane.showMessageDialog(null, "CAMPO EM BRANCO");
            } else {

                if (view.getUsuario().getText().equals("Admin")) {
                    if (view.getCampoSenha().getText().equals("Admin")) {
                        limparCampos();
                        new DirGeralPresenter();

                    } else {
                        JOptionPane.showMessageDialog(null, "SENHA INCORRETA");
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "USUÁRIO INCORRETO");
                }
            }
        }

        limparCampos();

    }
}
